package com.windfone.web.action;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.StringUtil;
import com.windfone.model.Member;
import com.windfone.service.MemberService;

public class MemberLoginAction extends ActionSupport {
	private String loginName;
	private String password;
	private MemberService memberService;

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String getLoginName() {
		return loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String memberLogout() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    if ( session.getAttribute( "userBean") !=null)
	    		session.removeAttribute("userBean");
		return SUCCESS;
	}
	public String execute() throws Exception {
		if( password !=null && !password.equals("") ){
			String tmp = StringUtil.encode(password);
			if( isInteger (loginName) ){
				Member memberBean= memberService.loginByMobile(loginName, tmp);
				if ( memberBean != null ){
					HttpServletRequest request = ServletActionContext.getRequest();
				    HttpSession session = request.getSession();
				    session.setAttribute("userBean", memberBean);
				    int i=memberService.updateLastLogin(memberBean);
				}
			} else {
				Member memberBean= memberService.loginByLoginName(loginName, tmp);
				if ( memberBean != null ){
					HttpServletRequest request = ServletActionContext.getRequest();
				    HttpSession session = request.getSession();
				    session.setAttribute("userBean", memberBean);
				    int i=memberService.updateLastLogin(memberBean);
				}
			}
		}
		return SUCCESS;
	}
	
	private String giftId;
	public String getGiftId() {
		return giftId;
	}

	public void setGiftId(String giftId) {
		this.giftId = giftId;
	}

	public String orderLogin() throws Exception {
		boolean checkFlag = false;
		if( password !=null && !password.equals("") ){
			String tmp = StringUtil.encode(password);
			if( isInteger (loginName) ){
				Member memberBean= memberService.loginByMobile(loginName, tmp);
				if ( memberBean != null ){
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession session = request.getSession();
					session.setAttribute("userBean", memberBean);
					int i=memberService.updateLastLogin(memberBean);
					checkFlag = true;
				}
			} else {
				Member memberBean= memberService.loginByLoginName(loginName, tmp);
				if ( memberBean != null ){
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession session = request.getSession();
					session.setAttribute("userBean", memberBean);
					int i=memberService.updateLastLogin(memberBean);
					checkFlag = true;
				}
			}
		}
		if ( checkFlag )
			return SUCCESS;
		else 
			return INPUT;
	}

	private  boolean isInteger(String value) {
		try {
			new BigInteger(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

}
